import { defineStore } from "pinia";
import { goodsService } from "@/services/goodsService";
import { ElMessage } from 'element-plus'

import type { List, GoodsItem } from "../types/goods.d.ts"

export const goodsStore = defineStore('goodsStore', {
    state: () => ({
        goodsData: {} as List<GoodsItem>
    }),

    actions: {
        /**  查询 */
        async getGodes(params = {}) {
            this.goodsData = {} as List<GoodsItem>

            const { code, data } = await goodsService.getGoodesService(params)

            if (code) {
                ElMessage.error('查询失败.')
            } else {
                this.goodsData = data
            }

        },

        /**  删除 */
        async deleteGoods(params: { productId: string | number }) {

            const { code } = await goodsService.deleteGoodsService(params)

            ElMessage({
                // message: `删除${message}`,
                message: `删除${code ? '失败' : '成功'}`,
                type: code ? 'warning' : 'success',
            })

            return { code }

        },



        /**  增加 */
        async createGoods(params: GoodsItem) {
            const { code } = await goodsService.createGoodsService(params)

            ElMessage({
                // message: `添加${message}`,
                message: `添加${code ? '失败' : '成功'}`,
                type: code ? 'warning' : 'success',
            })

            return { code }

        },

        /**  更新 */
        async updateGoods(params: GoodsItem) {
            const { code } = await goodsService.updateGoodsService(params)

            ElMessage({
                // message: `更新${message}`,
                message: `更新${code ? '失败' : '成功'}`,
                type: code ? 'warning' : 'success',
            })

            return { code }

        }
    }
})